package com.zzty.zzlp.controller;

import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.UserService;
import com.zzty.zzlp.socket.SocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class HelloController {

    @Autowired
    private SocketHandler socketHandler;

    @RequestMapping(path ="/login", method = RequestMethod.POST)
    public User login(@RequestBody User user, HttpServletRequest req) {
        //TODO: validate user

        User userDB=new User();
        req.getSession().setAttribute("user",userDB);
        return user;
    }

    @RequestMapping("/")
    public ModelAndView home() {
        ModelAndView m = new ModelAndView("/html/login.html");
        return m;
    }

    @RequestMapping(value = "/message", method = RequestMethod.GET)
    public String sendMessage(){

        socketHandler.sendMessageToUser("111", new TextMessage("这是一条测试的消息"));

        return "message";
    }

}
